#!/bin/sh
set -x

#脚本当前目录,引入配置文件
baseDirForScriptSelf=$(cd "$(dirname "$0")"; pwd)
source ${baseDirForScriptSelf}/config.properties

export ORACLE_BASE=${oracle_base}
export ORACLE_HOME=${oracle_home}
export PATH=$PATH:$HOME/bin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/oracle/bin:$ORACLE_HOME/bin:$PATH
export ORACLE_OWNER=oracle
export LD_LIBRARY_PATH=:$ORACLE_HOME/Slib
export NLS_LANG=American_america.zhs16gbk
export ORACLE_TERM=xterm
export AGENT_HOME=$ORACLE_BASE/agent10g

filepath=$1
taskid=$2
## 1-add 2-del
operationtype=$3
operator=$4
home_dir=$5


#define log file
batchtablename=tmp_keyword
plog=${home_dir}/plog.log
task_dir=${home_dir}/`date '+20%y%m%d'`/${taskid}

if [ ! -d "${task_dir}" ]; then
  mkdir -p "${task_dir}"
fi

echo `date '+20%y%m%d'`/${taskid}"***keyword********************************************" >> ${plog}
echo "parameter: filepath:"${filepath}" taskid:"${taskid}" operationtype:"${operationtype}" operator:"${operator}>> ${plog}
echo "[STARTING]:: $0 RUN Start at time:`date '+%Y-%m-%d_%H:%M:%S'`" >> ${plog}

echo "filePath:"${filepath} >> ${plog}
echo "fileRecordNumber:`[  -f ${filepath} ] && cat ${filepath}|grep -v ^$ | wc -l`" >>  ${plog}

cd ${home_dir}
##----------------------------------------------
#               check parameter
##----------------------------------------------
checkparameter(){
 echo "[1]::Starting checkparameter().........."  >> ${plog}


 if [ ! -f ${filepath} ]; then
   echo "ERROR:checkparameter Fail!!! ---file or filepath doesn't exists!!"  >> ${plog}
   return  1;
 fi 

 echo "[1]::Checkparameter() Successfull!!" >> ${plog}
}

##------------------------------------------------
#  create table
##----------------------------------------------
create(){
CMDSQL="${oracle_home}/bin/sqlplus -s /nolog <<EOF
connect ${USERDB_USERNAME}/${USERDB_PASSWD}@${USERDB_HOST}:1521/${USERDB_SID};
set feedback off;
set heading off;
set trimspool on;

create table ${batchtablename}
(
  keyword        VARCHAR2(200) not null
)	;
commit;
spool off;
"
echo $CMDSQL  >>  ${plog}
sh -c "${CMDSQL}"
}

##----------------------------------------------
#               sqlldr
##----------------------------------------------

doload(){
 echo "[3]::Starting doload().......... at time:`date '+%Y-%m-%d_%H:%M:%S'`"  >> ${plog}
#生成要导入的分表的ctl
ctl_file=${task_dir}/${taskid}_${cur_time}.ctl
bad_file=${task_dir}/${taskid}_${cur_time}.bad
dsc_file=${task_dir}/${taskid}_${cur_time}.dsc
log_file=${task_dir}/insert_${cur_time}.log
cat > ${ctl_file} << EOF
LOAD DATA
INFILE '${filepath}'
BADFILE '${bad_file}'
DISCARDFILE '${dsc_file}'

INTO TABLE ${batchtablename} append
(
keyword position(1:100)
)
EOF

echo 'ctl_file='${ctl_file}

#导入数据
sqlldr userid=${USERDB_USERNAME}/${USERDB_PASSWD}@${USERDB_HOST}:1521/${USERDB_SID} parallel=true control=${ctl_file} errors=10000000 rows=300 bindsize=2560000 log=${log_file}

echo "[3]::doload() Successfull  at time:`date '+%Y-%m-%d_%H:%M:%S'`!!"  >>  ${plog}

}

##-----------------------------------------------------
# do import db
##-----------------------------------------------------

doimport(){

echo "[4]::Starting doimport().......... at time:`date '+%Y-%m-%d_%H:%M:%S'`"  >> ${plog}

CMDSQL="${oracle_home}/bin/sqlplus -s /nolog <<EOF
conn ${USERDB_USERNAME}/${USERDB_PASSWD}@${USERDB_HOST}:1521/${USERDB_SID};
set feedback off;
set heading off;
set trimspool on;
--delete from temptable which KEYWORD is same
delete from ${batchtablename} a where rowid > (select min(rowid) from ${batchtablename} b where a.KEYWORD=b.KEYWORD);
--delete from temptable where KEYWORD in GWCMP_KEYWORD
 delete from ${batchtablename} where KEYWORD in (select KEYWORD from GWCMP_KEYWORD);
commit;

--insert into GWCMP_KEYWORD
insert into GWCMP_KEYWORD
select a.*,'${operator}',sysdate from ${batchtablename} a;
commit;

drop table ${batchtablename} purge;

spool off;
"
echo $CMDSQL  >>  ${plog}
sh -c "${CMDSQL}"

echo "[4]::doimport() Successfull  at time:`date '+%Y-%m-%d_%H:%M:%S'`!!"  >>  ${plog}

}

##----------------------------------------------------
#  do del
##-----------------------------------------------------

dodel(){

echo "[4]::Starting dodel().......... at time:`date '+%Y-%m-%d_%H:%M:%S'`"  >> ${plog} 

CMDSQL="${oracle_home}/bin/sqlplus -s /nolog <<EOF
conn ${USERDB_USERNAME}/${USERDB_PASSWD}@${USERDB_HOST}:1521/${USERDB_SID};
set feedback off;
set heading off;
set trimspool on;
--delete from GWCMP_KEYWORD where KEYWORD in batchtablename
delete from GWCMP_KEYWORD where KEYWORD in (select KEYWORD from ${batchtablename});
commit;

drop table ${batchtablename} purge;

spool off;
"
echo $CMDSQL  >>  ${plog} 
sh -c "${CMDSQL}"

echo "[4]::dodel() Successfull  at time:`date '+%Y-%m-%d_%H:%M:%S'`!!"  >>  ${plog} 

}


##====================================================
##main 
##====================================================
checkparameter;
if [ $? -eq 0 ]; then
create;
doload;
if [ $operationtype -eq 1 ]; then
	doimport;
else
	dodel;
fi 
fi

##
##
